package com.service.impl;

import com.dao.BuyerRepository;
import com.entity.*;
import com.service.BuyerService;
import com.util.CreateIdentity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private BuyerRepository buyerRepository;

    @Override
    public List<ProductCategory> findAllProductCatetory() {
//        findAllByTypeAndParentId
        List<ProductCategory> list1 = buyerRepository.findAllByTypeAndParentId(1, 0);
        for (ProductCategory productCategory:list1){
            int i=0;
            i++;
            List<ProductCategory> allByTypeAndParentId = buyerRepository.findAllByTypeAndParentId(2, productCategory.getId());
            //分割商品分类为三个list集合   未实现
            /*for (int i=0;i<6;i++){
                buyerRepository.findAllByTypeAndParentId(2, productCategory.getId());
            }*/
            if(i<=5){
                List<ProductCategory> list2 = allByTypeAndParentId;
                productCategory.setProductCategorieList(list2);
            }else if(i>5&&i<=11){
                List<ProductCategory> list2 = allByTypeAndParentId;
                productCategory.setProductCategorieList1(list2);
            }else{
                List<ProductCategory> list2 = allByTypeAndParentId;
                productCategory.setProductCategorieList2(list2);
            }

        }

        return list1;
    }

    @Override
    public List<Product> findAllProduct() {
        return buyerRepository.findAllProduct();
    }

    @Override
    public Product findProductDetail(Integer id) {
        return buyerRepository.findProductDetail(id);
    }

    @Override
    public List<Cart> findAllCartByLId(Integer LId) {

        return buyerRepository.findAllCartByLId(LId);
    }

    @Override
    public void addCartInfoByPId(Cart cart) {
        buyerRepository.addCartInfoByPId(cart);
    }

    @Override
    public void deleteCartByLId(Integer id, Integer LId) {
        buyerRepository.deleteCartByLId(id,LId);
    }

    @Override
    public List<Address> findAllAddress(Integer LId) {
        return buyerRepository.findAllAddress(LId);
    }

    @Override
    public Integer findSumCartByLId(Integer LId) {
        return buyerRepository.findSumCartByLId(LId);
    }

    @Override
    public Order addOrderInfoByLId(String address, Integer sum, Integer LId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Order order = new Order(LId,address,sum,sdf.format(new Date()),new CreateIdentity().createOrderIdentity());
        buyerRepository.addOrderInfoByLId(order);
        //增加订单列表后查询购物车List
        List<Cart> cartList = this.findAllCartByLId(LId);
        //查询最后一次订单表中的订单信息
        Order lastInfoByOrder = buyerRepository.findLastInfoByOrder(LId);
        //将其全部信息储存到order_detail表中
        for (Cart carts:cartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(lastInfoByOrder.getId());
            orderDetail.setProductId(carts.getProduct().getId());
            orderDetail.setQuantity(carts.getQuantity());
            orderDetail.setCost(lastInfoByOrder.getCost());
            buyerRepository.addOrderDetailInfo(orderDetail);
        }
        //清空购物车已经支付的订单信息
        buyerRepository.emptyCartInfo(LId);

        return lastInfoByOrder;
    }

    @Override
    public void addAddressInfoByLId(String allAddress, Integer LId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Address address = new Address(LId,allAddress,sdf.format(new Date()),"默认备注");
        buyerRepository.addAddressInfoByLId(address);
    }

    @Override
    public void deleteAddressInfo(Integer id, Integer LId) {
        buyerRepository.deleteAddressInfo(id,LId);
    }


}
